﻿using System;
using System.IO;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;


namespace Besture.UI.Admin.Content
{
    public partial class PartSet : BasePage
    {

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData();
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            //图片控件属性加载
            //ControlUpoadSinglePhoto.FolderPath = ImageHelper.PartPath;
            ControlSort.EditPage = EditPage;
            //加载栏目类型
            ddlType.DataSource = QueryDac.GetTable("SELECT * FROM TBDictionary WHERE FlagType = 2");
            ddlType.DataTextField = "Name";
            ddlType.DataValueField = "id";
            ddlType.DataBind();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        protected void InitData()
        {
            if (ID <= 0)
            {
                return;
            }
            var dac = new PartDac();
            var model = dac.GetModel(ID);

            Name.Text = model.Name;
            Remark.Text = model.Remarks;
            ControlSort.SortValue = model.SortID;
            ControlUpoadSinglePhoto.ImageUrl = model.ImageUrl;

            ControlPart.ParentId = model.ParentID;
            //初始栏目类型
            ddlType.SelectedValue = model.TypeID.ToString();
            //SEO
            ControlSeo.SeoTitle = model.SeoTitle;
            ControlSeo.SeoKeywords = model.SeoKeywords;
            ControlSeo.SeoDescription = model.SeoDescription;

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            PartDac PartDac = new PartDac();
            PartModel PartModel;
            BaseDac<PartModel> baseDac = new BaseDac<PartModel>();
            //添加
            if (!EditPage)
            {
                PartModel = new PartModel
                                     {
                                         Name = Name.Text,
                                         ParentID = ControlPart.ParentId,
                                         Remarks = Remark.Text,
                                         SortID = ControlSort.SortValue,
                                         ImageUrl = ControlUpoadSinglePhoto.ImageUrl,
                                         TypeID = Convert.ToInt32(ddlType.SelectedValue)
                                     };
                //加载SEO数据
                baseDac.LoadSeo(PartModel, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);
                //加载基本数据
                baseDac.LoadAddBaseModel(PartModel);
                //添加
                PartDac.Add(PartModel);

              
            }
            //编辑
            else
            {
                PartModel = PartDac.GetModel(ID);
                PartModel.Name = Name.Text;
                PartModel.ParentID = ControlPart.ParentId;
                PartModel.Remarks = Remark.Text;
                PartModel.SortID = ControlSort.SortValue;
                PartModel.ImageUrl = ControlUpoadSinglePhoto.ImageUrl;
                PartModel.TypeID = Convert.ToInt32(ddlType.SelectedValue);

                //加载SEO数据
                baseDac.LoadSeo(PartModel, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);
                //加载基本数据
                baseDac.LoadUpdateBaseModel(PartModel);
                PartDac.Update(PartModel);

                
            }
            RedirectListUrl(true);
        }
        #endregion
    }
}